package com.gxljc.bear.crawler.weixin;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxljc.bear.crawler.base.DolphinCrawlerConsts;
import com.gxljc.bear.crawler.base.DolphinFetchData;
import com.gxljc.bear.crawler.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 新榜微信文章列表数据爬虫。
 *
 * @author tanghaitao
 * @since 2022-10-9
 */
public class NewRankPageSeedCrawler implements Serializable {
    public static Logger LOG = Logger.getLogger(NewRankPageSeedCrawler.class);
    private Boolean proxy = false;
    private Boolean updateAll = false;

    public NewRankPageSeedCrawler(Boolean proxy, Boolean updateAll) {
        this.updateAll = updateAll;
        this.proxy = proxy;
    }

    //单条url入口
    public int crawl(AccountTable table) throws Exception {
        LOG.info("id = " + table.accountId + ",uuid = " + table.uuid);
        NewRankNonceInfo info = NewRankNonceInfo.getInstance();
        String url = info.getUrl(table);
        DolphinFetchData fetchData = DolphinFetchData.getInstance(DolphinCrawlerConsts.CrawlerChannelType.MOVIE.getName());
        byte[] htmlByte = fetchData.getHtml(url, proxy, WeixinConst.CRAWL_TIMEOUT);
        if (htmlByte == null) {
            LOG.error("new rank page result is empty url = " + url);
            return DolphinCrawlerConsts.RESULT_NO;
        }
        String html = new String(htmlByte);
        List<PageTable> pages = extract(table, html);
        WeixinMongodbUtil.savePageMongodb(pages, WeixinConst.MONGODB_NEWRANK_PAGE);
        return DolphinCrawlerConsts.RESULT_YES;
    }

//    private String genUrl(String uuid) {
//        String[] a = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
//        String nonce = "";
//        for (int d = 0; d < 9; d++) {
//            double ed = Math.floor(16 * Math.random());
//            int e = new Double(ed).intValue();
//            nonce += a[e];
//        }
//        System.out.println("nonce="+nonce);
//        String xgxljc = MD5Util.getMd5(nonce).toLowerCase();
//        //28a5a1f1d&xgxljc=e4bbfd29e9a18302dcdca4524fd1f9e8
//        String url = String.format(WeixinConst.NEWRANK_PAGE_URL, uuid, "28a5a1f1d", "e4bbfd29e9a18302dcdca4524fd1f9e8");
//        return url;
//    }

    //抽取
    private List<PageTable> extract(AccountTable account, String html) {
        final Gson GSON = new Gson();
//        System.out.println("html = " + html);
        Map map = GSON.fromJson(html, new TypeToken<Map>() {
        }.getType());
        if (map == null || map.isEmpty()){
            System.out.println("map is null==="+account.accountId);
            return null;
        }
        Object value = map.get("value");
        if (value == null) return null;
        Object listObj = ((Map) value).get("lastestArticle");
        if (listObj == null) return null;
        List<Map> list = (List<Map>) listObj;
        List<PageTable> tableList = new LinkedList<>();
        for (Map article : list) {
            PageTable table = null;
            try {
                table = genTableFromPage(account, article);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (table == null) continue;
            tableList.add(table);
        }
        return tableList;
    }

    //生成pageTable
    private PageTable genTableFromPage(AccountTable account, Map article) throws Exception {
        PageTable table = new PageTable();
        String pageId = (String) article.get("id");
        String messageId = (String) article.get("messageId");
        String title = (String) article.get("title");
        String summary = (String) article.get("summary");
        String original = (String) article.get("originalFlag");
        String publicTimeStr = (String) article.get("publicTime");
        String url = (String) article.get("url");
        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(title)) return null;
        table.setAccountId(account.accountId);
        table.setAccountName(account.name);
        table.setCrawlUrl(url);
        table.setPageId(pageId);
        table.setMessageId(messageId);
        table.setTitle(title);
        table.setSummary(summary);
        table.setOriginal(Integer.parseInt(original));
        table.setPublishDate(DateUtil.parseFormat(publicTimeStr, "yyyy-MM-dd HH:mm:ss"));
        table.setWid(WeixinUtil.genWid(table));
        if (updateAll) table.setCrawlFlag(0); //更新所有文章，默认为不更新
        return table;
    }


}
